import {mapGetters, mapMutations, mapActions} from 'vuex'
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {getMusicList} from 'api/rank'
import {createSong} from 'common/js/song'

export const playlistMixin = {//所有scllo组件的底部位置60
  computed: {
    ...mapGetters([
      'playlist'
    ])
  },
  mounted() {
    this.handlePlaylist(this.playlist)
  },
  activated() {//keep-alive组件激活
    this.handlePlaylist(this.playlist)
  },
  watch: {
    playlist(newVal) {
      this.handlePlaylist(newVal)
    }
  },
  methods: {
    handlePlaylist() {
      throw new Error('component must implement handlePlaylist method')
    }
  }
}

export const playerMixin = { //player
  computed: {
		iconMode(){//播放模式class
			if(this.mode==playMode.sequence){//0
				return "icon-sequence"
			}else if(this.mode==playMode.loop){//1
				return "icon-loop"
			}else{
				return "icon-random"
			}
		},
    ...mapGetters([
      'sequenceList',
      'playlist',
      'currentSong',
      'mode',
      'favoriteList'
    ])
  },
  methods: {
    changeMode() {
      const mode = (this.mode + 1) % 3
      this.setPlayMode(mode)
      let list = null
      if (mode === playMode.random) {
        list = shuffle(this.sequenceList)
      } else {
        list = this.sequenceList
      }
      this.resetCurrentIndex(list)
      this.setPlaylist(list)
    },
    resetCurrentIndex(list) {
      let index = list.findIndex((item) => {
        return item.id === this.currentSong.id
      })
      this.setCurrentIndex(index)
    },
    toggleFavorite(song) {//添加喜欢歌曲
      if (this.isFavorite(song)) {
        this.deleteFavoriteList(song)
      } else {
        this.saveFavoriteList(song)
      }
    },
    getFavoriteIcon(song) { //收藏歌曲图标
      if (this.isFavorite(song)) {
        return 'icon-favorite'
      }
      return 'icon-not-favorite'
    },
    isFavorite(song) {//喜欢歌曲是否在喜欢的列表
      const index = this.favoriteList.findIndex((item) => {
        return item.id === song.id
      })
      return index > -1
    },
    ...mapMutations({
      setPlayMode: 'SET_PLAY_MODE',
      setPlaylist: 'SET_PLAYLIST',
      setCurrentIndex: 'SET_CURRENT_INDEX',
      setPlayingState: 'SET_PLAYING_STATE',
			CURRENTSONG:'CURRENTSONG',
    }),
    ...mapActions([
      'saveFavoriteList',
      'deleteFavoriteList'
    ])
  }
}

export const searchMixin = {
  data() {
    return {
      query: '',
      refreshDelay: 120
    }
  },
  computed: {
    ...mapGetters([
      'searchHistory'
    ])
  },
  methods: {
    onQueryChange(query) {//box组件传递的数据
      return this.query = query
    },
    blurInput() {//失去焦点
      this.$refs.searchBox.blur()
			this.$refs.shortcut.refresh()
    },
    addQuery(query) {
      this.$refs.searchBox.setQuery(query)
    },
    saveSearch() {
      this.saveSearchHistory(this.query)
    },
    ...mapActions([
      'saveSearchHistory',
      'deleteSearchHistory'
    ])
  }
}

export const playpop = { //判断播放时机
  computed: {
    ...mapGetters([
      'dianji'
    ])
  },
  mounted() {
    let thta=this;
		function autoPlayMusic() {
				// 自动播放音乐效果，解决浏览器或者APP自动播放问题
				function musicInBrowserHandler() {
						musicPlay(true);
						document.body.removeEventListener('touchstart', musicInBrowserHandler);
				}
				document.body.addEventListener('touchstart', musicInBrowserHandler);
				// 自动播放音乐效果，解决微信自动播放问题
				function musicInWeixinHandler() {
						musicPlay(true);
						document.addEventListener("WeixinJSBridgeReady", function () {
								musicPlay(true);
						}, false);
						document.removeEventListener('DOMContentLoaded', musicInWeixinHandler);
				}
				document.addEventListener('DOMContentLoaded', musicInWeixinHandler);
			}
		function musicPlay(isPlay) {
					var audio = document.getElementById('music_mp3_0');
					if (isPlay && audio.paused) {
							audio.play();
					}
					if (!isPlay && !audio.paused) {
							audio.pause();
					}
			}
    if(thta.dianji>0){ //判断播放时机
    		autoPlayMusic();
    }else{
    	thta.pcoc(1)
			function musicIn() {
				let musicEle0 = document.getElementById('music_mp3_0');
						musicEle0.play();
						document.body.removeEventListener('touchstart', musicIn);
				}
				document.body.addEventListener('touchstart', musicIn);
				console.log("pppsssss")
		}
  },
	methods: {
		 ...mapMutations({
			  pcoc:'pcoc' //点击进入页面
			})
	 }
  
}

